package com.aaa.service.Impl;

import com.aaa.mapper.EmpExporMapper;
import com.aaa.mapper.EmpLogMapper;
import com.aaa.mapper.EmpMapper;
import com.aaa.pojos.*;
import com.aaa.service.EmpLogService;
import com.aaa.service.EmpService;
import com.aaa.util.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.jsonwebtoken.Jwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpMapper empMapper;
    @Resource
    private EmpExporMapper empExporMapper;
    @Resource
    private EmpLogService empLogService;


    @Override
    public PackBean<Emp> pageQuery(String name, Integer gender, LocalDate begin, LocalDate end, int page, int pageSize) {
//       没用PageHelper插件做法
//        int count = empMapper.count(name,gender,begin,end);
//        int i，ndex = (page-1)*pageSize;
//        List<Emp> list = empMapper.pageQuery(name,gender,begin,end,index,pageSize);
//        return new PackBean<Emp>(count,list);
//        return new PackBean<>(count,list);
        PageHelper.startPage(page,pageSize);
//        List<Emp> list = empMapper.page(name,gender,begin,end);
//        Page<Emp> empPage = (Page<Emp>) list;

        Page<Emp> empPage = (Page<Emp>) empMapper.page(name,gender,begin,end);
        return new PackBean<>((int) empPage.getTotal(),empPage.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEmp(Emp emp) throws Exception {
        try {
            log.info("添加员工信息{}"+emp);
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.addEmp(emp);
//        if (true){
////            人造编译时异常
//            throw new Exception("添加员工信息异常");
//        }
            List<EmpExpr> empExprList = emp.getExprList();
            log.info("添加员工经历{}"+empExprList);
            if (emp.getExprList() != null && !emp.getExprList().isEmpty()) {
                for (EmpExpr empExpr : empExprList) {
                    log.info("id{}"+emp.getId());
                    empExpr.setEmpId(emp.getId());
                }
                empExporMapper.insertEmpExpr(empExprList);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
            empLogService.insertLog(empLog);
        }
    }

    @Override
    public List<Emp> jobName() {
        log.info("查询所有职位");
       List<Emp> list = empMapper.jobName();
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmp(Integer[] ids) {
//        删除员工
        empMapper.deleteId(ids);
//        删除员工经历
        empExporMapper.deleteEmpId(ids);
    }

    @Override
    public Emp queryId(Integer id) {
        Emp emp = empMapper.selectId(id);
        return emp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Emp emp) {
//        更改修改时间
        emp.setUpdateTime(LocalDateTime.now());
//        修改Id员工信息
        empMapper.update(emp);
//        删除经历表
        log.info("删除经历表{}" + emp.getId());
        empExporMapper.deleteEmpId(new Integer[]{emp.getId()});
//        新增经历表
        List<EmpExpr> empExprList = emp.getExprList();
        if (empExprList != null && !empExprList.isEmpty()) {
            for (EmpExpr empExpr : empExprList) {
            empExpr.setEmpId(emp.getId());
            }
            empExporMapper.insertEmpExpr(empExprList);
        }
    }

    @Override
    public Login empLog(Emp emp1) {
        Emp emp=empMapper.empLog(emp1);
        if (emp != null) {
            Map<String,Object> totls=new HashMap<>();
            totls.put("id",emp.getId());
            totls.put("name",emp.getName());
            totls.put("username",emp.getUsername());
            String token = JwtUtils.generateJwt(totls);
            return new Login(emp.getId(),emp.getUsername(),emp.getName(),token);
        }
        return null;
    }

}
